package cn.lyjuan.payment.third.base.model.chl.domain.impl;

import cn.lyjuan.payment.third.base.common.entity.chl.ChlWxopenEntity;
import cn.lyjuan.payment.third.base.common.enums.impl.ChlEnum;
import cn.lyjuan.payment.third.base.common.mapper.ChlWxopenEntityMapper;
import cn.lyjuan.payment.third.base.model.chl.domain.IChlCnfModel;
import cn.lyjuan.payment.third.base.model.utils.FilePrefixUtil;
import cn.lyjuan.payment.third.base.pay.chl.impl.weixin.bean.WxChlCnf;
import cn.lyjuan.payment.third.base.pay.vo.req.ChlCnf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class WxOpenChlCnfModeImpl implements IChlCnfModel {
    @Autowired
    private ChlWxopenEntityMapper mapper;

    private ChlWxopenEntity entity;

    @Value("${certLocalPath}")
    private String CERT_PATH;

    @Override
    public ChlEnum chl() {
        return ChlEnum.WX_OPEN;
    }

    @Override
    public boolean query(Integer appId) {
        Example ex = new Example(ChlWxopenEntity.class);
        ex.createCriteria()
                .andEqualTo("appId", appId);

        this.entity = mapper.selectOneByExample(ex);

        if (null == this.entity) return false;

        return true;
    }

    @Override
    public ChlCnf pkg() {
        WxChlCnf cnf = new WxChlCnf();

        cnf.setChlType(chl().getSimpleChlEnum());
        cnf.setAppId(this.entity.getWxAppId());
        cnf.setMchId(this.entity.getMchid());
        cnf.setKey(this.entity.getAppkey());
        cnf.setCertPath(FilePrefixUtil.getPrefixPath(CERT_PATH));

        return cnf;
    }
}
